package com.bps.zzant.json;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bps.zzant.model.Customer;
import com.bps.zzant.model.Product;
import com.bps.zzant.services.ProductService;

@Controller
public class ProductJSON {
	private static final Logger logger = LoggerFactory.getLogger(ProductJSON.class);
	
	@Autowired
	private ProductService productService;
	// this is used in Order Entry auto complete function 
	@RequestMapping(value = "getProductByStyleNo.json", method = RequestMethod.GET)
	public Map getProductByStyleNo(HttpServletRequest request, HttpServletResponse response){
		
		Enumeration<String> a = request.getParameterNames();
	    for (; a.hasMoreElements(); ) {
	        logger.info("????????????"+(String)a.nextElement());
	    }
	        
	        
		logger.info(" +++++++++in ProductJSON.getProductByStyleNo styleNo:"+ request.getParameter("styleNo"));

		String styleNo = request.getParameter("styleNo");
		if(styleNo==null){
			styleNo="";
		}else {
			styleNo=styleNo+"%";
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Product> productList = productService.getProductByStyleNo(styleNo);
		logger.info(" +++++++++in ProductJSON.getProductByStyleNo productList size:"+productList.size());
		

		map.put("productList", productList);
		return map;
		
		
	}
	//this is used in product search
	@RequestMapping(value = "searchProduct.json", method = RequestMethod.GET)
	public Map searchProduct(HttpServletRequest request, HttpServletResponse response){
				
	    Map<String, Object> map = new HashMap<String, Object>(); 
	    List<Product> productList;
		String description = request.getParameter("description");
		String style = request.getParameter("style");
		logger.info("--------,,,, style 1: " + style);
		
		// When I use "scrollview" in the datagrid to display large dataset quickly, search function using datagrid 'load' event will call URL twice, one with
		// "page" parameter which I don't need that call. So when I see it, just return with nothing
		if(request.getParameter("page")!=null){
			map.put("success", new Boolean("true"));
			return map;
		}
		
		if(StringUtils.hasText(style)){
			if(StringUtils.hasText("*")){
				style = style.replace("*", "%");
				logger.info("--------,,,, style 2:" + style);
				
			}else if(!StringUtils.hasText("%")){
				logger.info("--------,,,, style 3:" + style);
				style = style.trim()+"%";
			}
			logger.info("--------,,,, style 4:" + style);
			productList = productService.getProductByStyleNo(style);
		}else if(StringUtils.hasText(description) || StringUtils.hasText(style)){
			logger.info(" +++++++++in ProductJSON.searchProduct. running description");
			productList = productService.getProductByDescription(description.trim());
		}else{
			productList = productService.getAllProducts();			
		}
		
		logger.info(" +++++++++in ProductJSON.searchProduct. productList size:"+productList.size());
		
		map.put("rows", productList);
		map.put("total", productList.size());
		map.put("success", true);

		return map;
		
		
	}
	
	@RequestMapping(value = "updateProduct.json", method = RequestMethod.POST)
	public Map updateProduct(@Valid Product product, BindingResult errors){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", new Boolean("true"));
		
		try {
			productService.updateProduct(product);
		} catch (Exception e) {
			map.put("success", new Boolean("false"));
		}
		
		return map;
	}
	
	@RequestMapping(value = "createProduct.json", method = RequestMethod.POST)
	public Map createProduct(@Valid Product product, BindingResult errors){
		
		logger.info(" +++++++++in createProduct.json:");
		
		logger.info(" +++++++++in updateProduct.json: id:"+ product.getProdId());
		logger.info(" +++++++++in updateProduct.json: brand:"+ product.getBrand());
		logger.info(" +++++++++in updateProduct.json: category:"+ product.getCategory());
		logger.info(" +++++++++in updateProduct.json: color:"+ product.getColor());
		logger.info(" +++++++++in updateProduct.json: description:"+ product.getDescription());
		logger.info(" +++++++++in updateProduct.json: page num:"+ product.getPageNum());
		logger.info(" +++++++++in updateProduct.json: size:"+ product.getSize());
		logger.info(" +++++++++in updateProduct.json: style no:"+ product.getStyleNo());
		
		logger.info(" +++++++++in updateProduct.json: cost:"+ product.getCost());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", new Boolean("true"));
		
		try {
			productService.saveProduct(product);
		} catch (Exception e) {
			map.put("success", new Boolean("false"));
		}
		
		return map;
	}
	
	
	
}
